package com.mf.controller.basic.webPage;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mf.aspect.debounces.Debounce;
import com.mf.aspect.logApi.LogApi;
import com.mf.exception.GraceException;
import com.mf.groups.GroupsAddInterface;
import com.mf.groups.GroupsUpdateInterface;
import com.mf.result.Result;
import com.mf.utils.UtilConverter;
import com.mf.utils.tree.UtilTree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import com.mf.entity.dto.basic.webPage.DtoWebMenu;
import com.mf.entity.req.basic.webPage.ReqWebMenu;
import com.mf.service.basic.webPage.ServiceWebMenu;
import com.mf.entity.req.basic.search.webPage.SearchReqWebMenu;

import java.util.List;

/**
 * 权限表
 */
@RestController
@RequestMapping("webMenu")
@Tag(name = "权限表")
@SaCheckLogin
@Debounce
public class ControllerWebMenu {
    @Resource
    private ServiceWebMenu servicewebMenu;



    @Operation(summary = "树结构列表查询")
    @SaCheckPermission("webMenu:search")
    @PostMapping("/listTree")
    public Result<List<DtoWebMenu>> listTree(@RequestBody SearchReqWebMenu searchReqWebMenu) {
        SearchReqWebMenu params = new SearchReqWebMenu();
        params.setStatus(searchReqWebMenu.getStatus());
        return Result.success(UtilTree.filterTree(servicewebMenu.findListByParams(params),
                servicewebMenu.findListByParams(searchReqWebMenu)));
    }

    @Operation(summary = "保存")
    @SaCheckPermission("webMenu:edit")
    @LogApi("保存权限")
    @PostMapping("/save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqWebMenu reqWebMenu) {
        servicewebMenu.save(UtilConverter.converter(reqWebMenu, DtoWebMenu.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @SaCheckPermission("webMenu:edit")
    @LogApi("修改权限")
    @PostMapping("/update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqWebMenu reqWebMenu) {
        servicewebMenu.updateById(UtilConverter.converter(reqWebMenu, DtoWebMenu.class));
        return Result.success();
    }


    @Operation(summary = "删除")
    @SaCheckPermission("webMenu:delete")
    @LogApi("删除权限")
    @GetMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Integer id) {
        GraceException.display(servicewebMenu.countByPid(id) > 0, "有子节点不可直接删除");
        servicewebMenu.removeById(id);
        return Result.success();
    }
}
